/******************************************************************************
 * * ui_nvm.h - header of nvm function
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#ifndef _UI_NVM_H_
#define _UI_NVM_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cpluscplus */
#include <stdio.h>
#include "include/ui_nvm_interface.h"
#include "string.h"
#include "lvgl.h"

/* definition of section length */
#define NV_SECTION_UI_SETTINGS_LEN              (sizeof(nv_watch_settings_t))
#define NV_SECTION_UI_PHONE_LEN                 (sizeof(nv_watch_phone_t))
#define NV_SECTION_UI_PEDOMETER_LEN             (sizeof(nv_watch_pedometer_t))
#define NV_SECTION_UI_LAUNCHER_LEN              (sizeof(nv_watch_launcher_t))
#define NV_SECTION_UI_PHONEBOOK_LEN             (sizeof(nv_watch_phonebook_t))
#define NV_SECTION_UI_FACTORYMODE_LEN           (sizeof(nv_watch_factorymode_t))
#define NV_SECTION_UI_ALARM_LEN                 (sizeof(nv_watch_alarm_t))
#define NV_SECTION_UI_VOICE_MSG_LEN             (sizeof(nv_watch_voice_msg_t))
#define NV_SECTION_UI_ENGINEER_MODE_LEN         (sizeof(nv_watch_engineer_mode_t))
#define NV_SECTION_UI_BLUETOOTH_LEN             (sizeof(nv_watch_bluetooth_t))
#define NV_SECTION_UI_AUDIO_PLAYER_LEN          (sizeof(nv_watch_audio_player_t))
#define NV_SECTION_UI_DM_LEN                    (sizeof(nv_watch_dm_t))
#define NV_SECTION_UI_CALL_LOG_LEN              (sizeof(nv_watch_call_log_t))
#define NV_SECTION_LEN(sectionid)    (sectionid ## _LEN)

/* definition of section start address */

typedef lv_fs_file_t ui_file_t;
typedef lv_fs_dir_t  ui_dir_t;
#define UI_FILE_READ_ONLY     LV_FS_MODE_RD
#define UI_FILE_WRITE_ONLY    LV_FS_MODE_WR
#define UI_FILE_RD_APPEND     (LV_FS_MODE_RD | LV_FS_MODE_APPEND)
#define UI_FILE_APPEND        LV_FS_MODE_APPEND
#define UI_FILE_READ_WRITE    (LV_FS_MODE_RD | LV_FS_MODE_WR)
#define UI_FILE_OPEN(p, m)   \
    ({                       \
        ui_file_t *f;        \
        f = lv_mem_alloc(sizeof(ui_file_t)); \
        memset(f,0,sizeof(ui_file_t)); \
        lv_fs_open(f, p, m); \
        (f);                 \
    }                        \
    )

#define UI_FILE_CLOSE(f)      \
    ({                        \
        lv_fs_close(f);       \
        lv_mem_free(f);       \
    }                         \
    )
#define UI_FILE_SEEK          lv_fs_seek
#define UI_FILE_READ(f, b, l)          \
    ({                                 \
        uint32_t Ret_Len;                \
        lv_fs_read(f, b, l, &Ret_Len); \
        (Ret_Len);                     \
    }                                  \
    )

#define UI_FILE_WRITE(f, b, l)          \
    ({                                  \
        uint32_t Ret_Len;                 \
        lv_fs_write(f, b, l, &Ret_Len); \
        (Ret_Len);                      \
    }                                   \
    )

#define UI_FILE_TELL                             lv_fs_tell
#define UI_FILE_RENAME                           lv_fs_rename
#define UI_FILE_REMOVE                           lv_fs_remove
#define UI_FILE_SIZE                             lv_fs_size
#define UI_DIR_OPEN(p)        \
    ({                        \
        ui_dir_t *d;          \
        lv_fs_dir_open(d, p); \
        (d);                  \
    }                         \
    )

#define NV_SECTION_ROOT_DIR                      "C:/"

#define NV_MAX_SECTION_PATH_NAME                 50
#define NV_SECTION_UI_SETTINGS_FILE              "ui_settings.nv"
#define NV_SECTION_UI_PHONE_FILE                 "ui_phone.nv"
#define NV_SECTION_UI_PEDOMETER_FILE             "ui_pedometer.nv"
#define NV_SECTION_UI_LAUNCHER_FILE              "ui_launcher.nv"
#define NV_SECTION_UI_PHONEBOOK_FILE             "ui_phonebook.nv"
#define NV_SECTION_UI_FACTORYMODE_FILE           "ui_factorymode.nv"
#define NV_SECTION_UI_ALARM_FILE                 "ui_alarm.nv"
#define NV_SECTION_UI_VOICE_MSG_FILE             "ui_voicemsg.nv"
#define NV_SECTION_UI_ENGINEER_MODE_FILE         "ui_engineermode.nv"
#define NV_SECTION_UI_BLUETOOTH_FILE             "ui_bluetooth.nv"
#define NV_SECTION_UI_AUDIO_PLAYER_FILE          "ui_audioplayer.nv"
#define NV_SECTION_UI_DM_FILE                    "ui_dm.nv"
#define NV_SECTION_UI_CALL_LOG_FILE              "ui_calllog.nv"
#define NV_SECTION_FILE_PATH(sectionid)    (sectionid ## _FILE)

extern char *UI_NV_Get_Section_path(NV_UI_SECTION_ID SectionId);


#ifdef __cplusplus
}
#endif /* __cpluscplus */
#endif /* end of _UI_NVM_H_ */
